/**
 * Created by evil on 17-2-21.
 */
$(function ()
{
    base.init();
    base.load_list();
    base.table();
    base.dialog_submit();
    base.submit();
});

var base = {
    init: function ()
    {
        $('#setting-css').attr('href', '/static/css/setting_base.css');
    },
    load_list: function ()
    {
        $.get('/setting/get/list/seat', function (list)
        {
            var html;
            var body = $('.table tbody');
            body.empty();
            for (var i = 0; i < list.length; i++)
            {
                var seat = list[i];
                html = '<tr>';
                html += '<td><a href="javascript:base.seat_fix(' + seat.seatId + ')">'
                    + seat.seatId + '</a></td>';
                html += '<td>' + seat.seat + '</td>';

                if (seat.state == true)
                    html += '<td><button class="btn btn-xs btn-success" onclick="base.seat_close(' + seat.seatId + ',true)">开启中</button></td>';
                else
                    html += '<td><button class="btn btn-xs btn-warning" onclick="base.seat_close(' + seat.seatId + ',false)">关闭中</button></td>';

                body.append(html);
            }
        })
    },
    table: function ()
    {
        $('#add-seat').click(function ()
        {
            var id = 0;
            var dialog = $('#seat-dialog');
            dialog.modal('show');
            $.get('/setting/get/seat/last/id', function (lastId)
            {
                id = lastId + 1;
                dialog.find('h4').html('添加座位->ID：<span id="add-id">' + id + '</span>');
                dialog.find('.modal-body').html("<div class=\"input form-horizontal\"></div><div class=\"form-group\">\n" +
                    "            <label for=\"add-text\" class=\"col-sm-2 control-label\">座位名称</label>\n" +
                    "            <div class=\"col-sm-5\">\n" +
                    "                <input type=\"text\" class=\"form-control\" id=\"add-text\" placeholder=\"请输入座位名称\">\n" +
                    "            </div>\n" +
                    "        </div></div><br>");
            });
        });
    },
    seat_close: function (id, state)
    {
        $(this).addClass('disabled');

        $.post('/setting/post/seat/state', {
            id: id,
            state: !state
        }, function (data)
        {
            $(this).removeClass('disabled');
            if (data == true)
                base.load_list();
        });
    },
    seat_fix: function (id)
    {
        var dialog = $('#seat-dialog');
        dialog.modal('show');

        $.get('/setting/get/seat', {
            id: id
        }, function (data)
        {
            dialog.find('h4').html('修改座位->ID：<span id="add-id">' + data.seatId + '</span>');
            dialog.find('.modal-body').html("<div class=\"input form-horizontal\"></div><div class=\"form-group\">\n" +
                "            <label for=\"add-text\" class=\"col-sm-2 control-label\">座位名称</label>\n" +
                "            <div class=\"col-sm-5\">\n" +
                "                <input type=\"text\" class=\"form-control\" id=\"add-text\" value='" + data.seat + "'>\n" +
                "            </div>\n" +
                "        </div></div><br>");
        });
    },
    dialog_submit: function ()
    {
        $('#dialog-submit').click(function ()
        {
            var id = $('#add-id').html();
            var seat = $('#add-text').val();

            $.post('/setting/post/seat', {
                id: id,
                seat: seat,
                state: true
            }, function (data)
            {
                $('#seat-dialog').modal('hide');
                base.load_list();
            });
        });
    },
    submit: function ()
    {
        $('#submit').click(function ()
        {
            cleanInputHelp();

            var name = $('#name');
            var address = $('#address');
            var introduction = $('#introduction');
            var type = $('#type');

            var sum = 0;

            sum += isInputNull(name);
            sum += isInputNull(address);
            sum += isInputNull(introduction);
            sum += isInputNull(type, '请选择饭店类型！');

            if (sum == 4)
            {
                $(this).addClass('disabled');

                var url = '/setting/post/essential/submit';
                /*有图片时，更换地址*/
                if ($('#img').val() != '')
                    url += '_img';

                var options = {
                    url: url,
                    type: 'post',
                    success: function (data)
                    {
                        if (data == true)
                            toastr.success('提交成功，请刷新查看！');
                        else
                            toastr.error('出现错误，请检查后重试！');

                        $('#submit').removeClass('disabled');
                    }
                };

                $(".input").ajaxSubmit(options);
            }
        });
    }
};

function isInputNull(it, help)
{
    if (it.val() == null || it.val().toString().trim() == '')
    {
        it.parent().parent().addClass('has-error');
        if (help == null)
            it.parent().find('span').text('输入为空！');
        else
            it.parent().find('span').text(help);

        return 0;
    }
    return 1;
}

function cleanInputHelp()
{
    var input = $('.input');
    input.find('.form-group').removeClass('has-error');
    input.find('.help-block').empty();
}


/**
 * var ids = [];
 var seats = [];
 var states = [];

 var size = -1;
 var index = 0;

 var td = $('.table tr td');
 for (var i = 0; i < td.length; i++)
 {
     index = i % 3;
     if (index == 0)
         size++;

     switch (index)
     {
         case 0:
             ids[size] = $(td[i]).text();
             break;
         case 1:
             seats[size] = $(td[i]).html();
             break;
         case 2:
             if ($(td[i]).html().indexOf('开启中') > 0)
                 states[size] = 1;
             else
                 states[size] = 0;
             break;
     }
 }
 *
 *
 */
